import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'
//注册功能
import Register from './components/Register.vue'
import Home from './components/Home.vue'
import Welcome from './components/Welcome.vue'
import Users from './components/user/Users.vue'
import Rights from './components/power/Rights.vue'
import Cate from './components/goods/Cate.vue'
import Roles from './components/power/Roles.vue'
import Params from './components/goods/Params.vue'
import List from './components/goods/List.vue'
import Add from './components/goods/Add.vue'
import Order from './components/order/Order.vue'
//管理员查询快递
import queryAll from './components/express/queryAll.vue'
// 快递员承运的快递
import entryExpress from './components/express/entryExpress.vue'
// 寄件
import SendExpress from './components/sender/SendExpress.vue'
// 发布通知
import Notice from './components/inform/notice.vue'
import getInform from './components/inform/getInform.vue'

import Reports from './components/reports/reportsList.vue'
import Big from './components/big/index.vue'
Vue.use(Router)


const router = new Router({
    routes: [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login
    },
    {
        path: '/register',
        component: Register
    },
    // {
    //     path: '/big',
    //     component: Big
    // },
    {
        path: '/home',
        component: Home,
        redirect: '/welcome',
        children: [{
            path: '/welcome',
            component: Welcome
        },
        {
            path: '/users',
            component: Users
        },
        {
            path: '/big',
            component: Big
        },
        {
            path: '/search',
            component: Order
        },
        {
            path: '/SendExpress',
            component: SendExpress
        },
        {
            path: '/addExpress',
            component: SendExpress
        },
        {
            path: '/inform',
            component: Notice,
        },
        {
            path: '/getInform',
            component: getInform,
        },
        {
            path: '/roles',
            component: Roles
        },
        {
            path: '/categories',
            component: Cate
        },
        {
            path: '/params',
            component: Params
        },
        {
            path: '/goods',
            component: List,
        },
        {
            path: '/goods/add',
            component: Add
        },
        {
            path: '/string',
            component: Order
        },
        {
            path: '/reports',
            component: Reports
        },
        // 查询所有快递
        {
            path: '/searchAll',
            component: queryAll
        },
        // 查询承运的快递
        {
            path: '/selectByMeExpress',
            component: entryExpress
        }
        ]
    }
    ]
})

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

//判断是否登录
router.beforeEach((to, from, next) => {
    //next是一个函数，表示放行
    if (to.path == '/login' || to.path == '/register') return next()
    //获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})
export default router